#!/usr/bin/env python
# coding=utf-8

import random

class Wheel(object):
    def __init__(self):
        self.rng = random.Random()
        self.bins = [
            {str(n): (35,1),
             self.redblack(n): (1,1),
             self.hilo(n): (1,1),
             self.evenodd(n): (1,1)
            } for n in range(1,37)
        ]

    @staticmethod
    def redblack(n):
        return "Red" if n in (1,3,5,7,9,12,14,16,18,19,21,23,25,27,30,32,34,36) else "Black"
    @staticmethod
    def hilo(n):
        return "Hi" if n >=19 else "LO"
    @staticmethod
    def evenodd(n):
        return "Even" if n%2 == 0 else "Odd"
    def spin(self):
        return self.rng.choice(self.bins)


class Zero(Wheel):
    def __init__(self):
        super(Zero, self).__init__()
        self.bins += [ {'0': (35, 1)} ]

class DoubleZero(Wheel):
    def __init__(self):
        super(DoubleZero, self).__init__()
        self.bins += [ {'00': (35, 1)} ]

class American(Zero):
    pass

class European(Zero):
    pass

american = American()

import sys
import wsgiref.util
import json
import os
os.environ['PATH_INFO'] = "jiangkun"
def wheel(environ, start_response):
    request = wsgiref.util.shift_path_info(environ)
    str_out =  "wheel:%s, 123" %(request == "")
    os.system("echo %s > 1.txt" % str_out)
    winner = american.spin()
    status = "200 OK"
    headers = [('Content-type', 'application/json; charset=utf-8')]
    start_response(status, headers)
    return [json.dumps(winner).encode('UTF-8')]

from wsgiref.simple_server import make_server
def roulette_server(count=1):
    httpd = make_server('', 8080, wheel)
    if count is None:
        httpd.serve_forever()
    else:
        for c in range(count):
            httpd.handle_request()


if __name__ == "__main__":
    roulette_server()







